#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'libb@anchent.com'

import os
import time
from random import randint
from multiprocessing import Process, Pipe


def download_task(conn, name):
    print("download_task current_pid:%d" % os.getpid())
    download_time = randint(5, 10)
    time.sleep(download_time)
    conn.send(['download' + str(name)])
    print("%s download_task finished download_time:%d" % (name, download_time))


if __name__ == '__main__':
    start = time.time()
    process_list = []

    parent_conn, child_conn = Pipe()
    for i in range(10):
        p = Process(target=download_task, args=(child_conn, i))
        p.start()
        process_list.append(p)
    for j in process_list:
        j.join()
    for k in range(10):
        print(parent_conn.recv())
    end = time.time()
    print("this python_pipe total cost time: %.2f" % (end - start))
